package com.ruitu.mall_98_2.mine;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.support.glide.transformations.GlideImageLoader;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.utils.UIUtils;
import com.beanu.arad.widget.EditTextCleanView;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.hjq.permissions.Permission;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.BandingAlipayContract;
import com.ruitu.mall_98_2.mvp.model.BandingAlipayModel;
import com.ruitu.mall_98_2.mvp.presenter.BandingAlipayPresenter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.User;
import com.ruitu.router_module.bean.mine.AlipayInfo;
import com.ruitu.router_module.utils.PermissionUtil;

import java.io.File;
import java.util.ArrayList;

import okhttp3.MultipartBody;

public class BandingAlipayActivity extends MyBaseActivity<BandingAlipayPresenter, BandingAlipayModel> implements BandingAlipayContract.View {
    private EditTextCleanView clean_et_alipay_account;
    private EditTextCleanView clean_et_alipay_name;
    private EditTextCleanView clean_et_weixin_account;
    private EditTextCleanView clean_et_weixin_name;
    private EditTextCleanView clean_et_real_name;
    private EditTextCleanView clean_et_bank_account;
    private EditTextCleanView clean_et_bank_name;
    private TextView tv_banding;
    private String string;
    private LinearLayout ll_reason;
    private EditText et_reason;
    private String name;

    private ImageView iv_weixin_scan;
    private ImageView iv_alipay_scan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_banding_alipay;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        string = getIntent().getStringExtra("update");
        clean_et_alipay_account = findViewById(R.id.clean_et_alipay_account);
        clean_et_alipay_name = findViewById(R.id.clean_et_alipay_name);
        ll_reason = findViewById(R.id.ll_reason);
        tv_banding = findViewById(R.id.tv_banding);
        et_reason = findViewById(R.id.et_reason);
        clean_et_weixin_account = findViewById(R.id.clean_et_weixin_account);
        clean_et_weixin_name = findViewById(R.id.clean_et_weixin_name);
        clean_et_real_name = findViewById(R.id.clean_et_real_name);
        clean_et_bank_account = findViewById(R.id.clean_et_bank_account);
        iv_weixin_scan = findViewById(R.id.iv_weixin_scan);
        iv_alipay_scan = findViewById(R.id.iv_alipay_scan);
        clean_et_bank_name = findViewById(R.id.clean_et_bank_name);
        tv_banding.setOnClickListener(this);
        iv_weixin_scan.setOnClickListener(this);
        iv_alipay_scan.setOnClickListener(this);
        if (TextUtils.isEmpty(string)) {
            setHeadTitle("绑定收款信息");
        } else {
            setHeadTitle("申请修改收款信息");
            ll_reason.setVisibility(View.VISIBLE);
            tv_banding.setText("申请修改");
            mPresenter.findAlipay(AppHolder.instance().getUser().getAccountId());
        }
    }

    private File weixin_file;
    private File alipay_file;

    @Override
    public void onClick(View v) {
        super.onClick(v);

        if (iv_weixin_scan == v) {

            PermissionUtil.handleMultiPermission(this, "读写手机存储", new PermissionUtil.PermissionListener() {
                @Override
                public void onPermissionOk(boolean isOk) {
                    if (isOk) {
                        Intent intent = new Intent(BandingAlipayActivity.this, ImageGridActivity.class);
                        setMaxPage(1);
                        startActivityForResult(intent, weixin_req);
                    }
                }

                @Override
                public void onOpenPermissionPage(boolean isOpen) {
                }
            }, Permission.Group.STORAGE);

        }

        if (iv_alipay_scan == v) {

            PermissionUtil.handleMultiPermission(this, "读写手机存储", new PermissionUtil.PermissionListener() {
                @Override
                public void onPermissionOk(boolean isOk) {
                    if (isOk) {
                        Intent intent = new Intent(BandingAlipayActivity.this, ImageGridActivity.class);
                        setMaxPage(1);
                        startActivityForResult(intent, alipay_req);
                    }
                }

                @Override
                public void onOpenPermissionPage(boolean isOpen) {
                }
            }, Permission.Group.STORAGE);
        }
        if (tv_banding == v) {
            final String account = clean_et_alipay_account.getText().toString();
            name = clean_et_alipay_name.getText().toString();
            final String weixin_account = clean_et_weixin_account.getText().toString();
            final String weixin_name = clean_et_weixin_name.getText().toString();
            final String real_name = clean_et_real_name.getText().toString();
            final String bank_name = clean_et_bank_name.getText().toString();
            final String bank_account = clean_et_bank_account.getText().toString();
            if (TextUtils.isEmpty(account)) {
                ToastUtils.showShort("支付宝账号不能空");
                return;
            }

            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort("支付宝用户名不能为空");
                return;
            }

            if (TextUtils.isEmpty(real_name)) {
                ToastUtils.showShort("用户真实姓名不能为空");
                return;
            }
            if (!TextUtils.isEmpty(bank_account)){
                if (TextUtils.isEmpty(bank_name)){
                    ToastUtils.showShort("开户行不能为空");
                    return;
                }
            }
            String info = "账号:" + account + '\n'
                    + "用户名:" + name;
            UIUtils.showAlertDialog(getSupportFragmentManager(), "请确认账号信息", info,
                    "确定", "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            showProgress();
                            if (tv_banding.getText().equals("绑定")) {
                                MultipartBody body = mPresenter.bindAlipay(AppHolder.instance().getUser().getAccountId(), account, name, weixin_account, weixin_name,bank_name, real_name, bank_account, alipay_file, weixin_file);
                                mPresenter.bindAlipay(body);
//
                            } else {
                                MultipartBody multipartBody = mPresenter.modifyAlipay(AppHolder.instance().getUser().getAccountId(), account, name, et_reason.getText().toString(), weixin_account, weixin_name,bank_name, real_name, bank_account, alipay_file, weixin_file);
                                mPresenter.modifyAlipay(multipartBody);

                            }

                        }
                    }, null);

        }

    }

    private void setMaxPage(int i) {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setCrop(false);
        imagePicker.setSelectLimit(i);
        imagePicker.setSaveRectangle(true); //是否按矩形区域保存
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);   //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);  //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);//保存文件的高度。单位像素
    }


    @Override
    public void setBandingSuc() {
        User.AlipayUserInfo user = new User.AlipayUserInfo();
        user.setAlipayAccount(name);
        user.setAccountPhone(clean_et_alipay_account.getText().toString());
        AppHolder.instance().getUser().setAlipayUserInfo(user);
        finish();
    }

    @Override
    public void setUpdateSuc() {
        UIUtils.showAlertDialog(getSupportFragmentManager(), "等待管理人员审核",
                "", "确定", "", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        Arad.bus.post(new EventModel<String>(18121400, account));
                        finish();

                    }
                }, null);

    }

    @Override
    public void setAlipayInfo(AlipayInfo alipayInfo) {
        setAlipay(alipayInfo);
    }

    private void setAlipay(AlipayInfo alipayInfo) {
        clean_et_alipay_account.setText(alipayInfo.getAlipayNumber());
        clean_et_alipay_name.setText(alipayInfo.getAlipayAccount());
        clean_et_weixin_name.setText(alipayInfo.getWechatName());
        clean_et_weixin_account.setText(alipayInfo.getWechatNumber());
        clean_et_bank_account.setText(alipayInfo.getBankCard());
        clean_et_real_name.setText(alipayInfo.getRealName());
        clean_et_bank_name.setText(alipayInfo.getOpenBank());
        Glide.with(this).load(Config.BASE_URL + alipayInfo.getWechatImg()).apply(Arad.opts_company_defult_logo).into(iv_weixin_scan);
        Glide.with(this).load(Config.BASE_URL + alipayInfo.getAlipayImg()).apply(Arad.opts_company_defult_logo).into(iv_alipay_scan);
    }

    private int weixin_req = 979;
    private int alipay_req = 989;
    ArrayList<ImageItem> imageItems;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == weixin_req) {
            if (data != null) {
                imageItems = data.getParcelableArrayListExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                weixin_file = new File(imageItems.get(0).path);
                Glide.with(this).load(imageItems.get(0).path).into(iv_weixin_scan);
            }
        }

        if (requestCode == alipay_req) {
            if (data != null) {
                imageItems = data.getParcelableArrayListExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                alipay_file = new File(imageItems.get(0).path);
                Glide.with(this).load(imageItems.get(0).path).into(iv_alipay_scan);
            }
        }
    }
}
